package com.smec.moblie.control;

import android.util.Log;

import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;


public class HttpRequestUtil {
    /**
     * Json格式call Rest接口
     *
     * @param baseUrl
     * @param subUrl
     * @param jsonData
     * @return
     */
    public static String callRestUrlByJson(String baseUrl, String subUrl,
                                           String jsonData) {
        // int resultCode = INFO_SUCCEED;
        // int statusCode = 0;
        String retSrc = null;
        try {
            String restURIPre = baseUrl + subUrl;
            HttpPost httpPost = new HttpPost(restURIPre);
//			Log.d("httpPostUtil", "call Rest url:" + restURIPre + "  data:"
//					+ jsonData + "");
            if (jsonData != null) {
                StringEntity se = new StringEntity(jsonData);
                httpPost.setEntity(se);
            }

            httpPost.setHeader("Content-Type", "application/json");
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(httpPost);

            retSrc = EntityUtils.toString(response.getEntity());
            // 结果返回一个字符串，‘Success’ 或者 ‘Failed’+错误信息
            // statusCode = response.getStatusLine().getStatusCode();
            // if (statusCode >= 200 && statusCode < 300 &&
            // "Success".equals(retSrc)) {
            // resultCode = INFO_SUCCEED;
            // } else {
            // resultCode = ERR_BPM_WS;
            // }

//			Log.d("call Rest url:" + restURIPre + ",response:", retSrc);
        } catch (Exception e) {
            // resultCode = ERR_BPM_WS;
            Log.e("err", e.toString());
        }
        return retSrc;
    }

    /**
     * Get方法调用Rest接口
     *
     * @param baseUrl
     * @param subUrl
     * @param data
     * @return
     */
    public static String callRestUrlGet(String baseUrl, String subUrl,
                                        String data) {
        String retSrc = null;
        try {
            String restURIPre = baseUrl + subUrl + data;
            HttpGet httpGet = new HttpGet(restURIPre);
            Log.d("httpPostUtil", "call Rest url:" + restURIPre + "  data:"
                    + data + "");

            // httpGet.setHeader("Content-Type", "application/json");
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {

                retSrc = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
            Log.d("httpPostUtil", "call Rest url:" + restURIPre + ",response:"+retSrc);
        } catch (Exception e) {
            Log.e("err", e.getMessage());
        }
        return retSrc;
    }
}
